package com.study.springboot.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @author huyong
 * @Decription
 * @date 2025-03-28 15:07
 */
@Configuration
public class GatewayConfig {

    /**
     * 定义限流的键 这里是请求参数中的用户 也可以使用ip
     */
   // @Bean
    KeyResolver userKeyResolver() {
        return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getQueryParams().getFirst("user")));
    }

    /**
     * 根据ip限流
     */
    @Bean
    KeyResolver hostKeyResolver() {
        return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostName());
    }

    /**
     * 根据请求路径进行限流
     */
  //  @Bean
    KeyResolver pathKeyResolver() {
        return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getPath().value()));
    }
}
